<?php
class xitongAction extends CommonAction
{
    public function index()
    {
        $employee = M('employee');
        $this->list = $employee->select();
        $arr = $employee->find();
        $this->assign('employee', $arr);
        $this->display();
    }

    public function addren()
    {
        $department = M("department");
        $this->li = $department->select();
        $role = M("role");
        $this->list = $role->select();
        $this->display();
    }

    public function addemployee()
    {
        $employee = D('employee');
        $data = $employee->create();
        if (!$data) $this->error($employee->getError());

        if($data["role"]==9)
            $data["isadmin"]=1;

        $result = $employee->add($data);

        if (!$result) $this->error("添加失败");
        $this->assign("waitSecond", 1);
        $this->assign("jumpUrl", U("xitong/index"));
        $this->success("添加成功");


    }

    public function delren()
    {
        $id = I("get.id"); // $_GET['id'];
        if (empty($id)) $this->error("人员编号为空");

        $employee = M('employee');
        $condition["id"] = $id;
        $employeedata = $employee->where($condition)->find();
        if (!$employeedata) $this->error("没有找到相关人员");
        if ($employeedata["issys"]) $this->error("系统保留数据,不能删除");

        $count = $employee->delete($id);
        if ($count > 0)
        {
            $this->success("删除成功");
        } else
        {
            $this->error("删除失败");
        }
    }

    public function updateren()
    {
        $department = M("department");
        $this->li = $department->select();
        $role = M("role");
        $this->list = $role->select();
        $employee = M('employee');
        $id = $_GET['id'];
        $arr = $employee->find($id);
        $this->assign('employee', $arr);
        $this->display();
    }

    public function  changeren()
    {

        $employee = D('employee');
        $data = $employee->create();
        if (!$data) $this->error($employee->getError());

        if (empty($data["password"]))
        {
            unset($data["password"]);
        }

        $count = $employee->save($data);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/index"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function bumen()
    {
        $department = M('department');
        $data = $department->order("id asc")->select();
        $this->list = $data;
        $this->departlist = $data;
        $this->display();

    }

    public function  bumenchange()
    {
        $department = M('department');
        $data=$department->select();
        $this->departlist=$data;

        $id =I('get.id',0);// $_GET['id'];
        $arr = $department->find($id);
        $this->assign('department', $arr);
        $this->display();
    }

    public function  updatebumen()
    {

        $department = M('department');

        $proid = I('post.id', '', 'htmlspecialchars');
        $pro['id'] = $proid;
        $pro['name'] = I('post.name', '', 'htmlspecialchars');
        $pro['leader'] = I('post.leader', '', 'htmlspecialchars');
        $parentid = I('post.parentid');
        $pro['parentid'] = $parentid;
        $parentdata = $department->find($parentid);
        $parentpath = $parentdata["path"];
        if (empty($parentdata)) {
            $parentpath = "0,";
        }

        $pro["path"] = $parentpath . $proid . ",";

        $count = $department->save($pro);
        $sql = $department->getLastSql();
        //  echo "sql =$sql";exit();
        if ($count !== false) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/bumen"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function addbumen()
    {
        $department = M('department');
        $data['name'] = I('post.name', '', 'htmlspecialchars');
        $data['leader'] = I('post.leader', '', 'htmlspecialchars');
        $parentid = I('post.parentid');
        $data['parentid'] = $parentid;
        $deparentid = $department->add($data);


        if ($deparentid) {
            $parentcon["id"] = $parentid;
            $parentdata = $department->where($parentcon)->find();
            $path = $parentdata["path"];
            if (empty($path)) $path = "0,";
            $adddepartdata["path"] = $path . $deparentid . ",";
            $adddepartdata["id"] = $deparentid;
            $department->save($adddepartdata); //更新department




            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/bumen"));
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    public function delbumen()
    {
        $department = M('department');
        $id = $_GET['id'];
        $count = $department->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }


    public function company()
    {
        $company = M('company');
        $this->list = $company->select();
        $this->display();
    }

    public function comchange()
    {
        $company = M('company');
        $id = $_GET['id'];
        $arr = $company->find($id);
        $this->assign('company', $arr);
        $this->display();
    }

    public function comupdate()
    {
        $company = M('company');
        $com['id'] = I('post.id', '', 'htmlspecialchars');
        $com['name'] = I('post.name', '', 'htmlspecialchars');
        $com['person'] = I('post.person', '', 'htmlspecialchars');
        $com['telephone'] = I('post.telephone', '', 'htmlspecialchars');
        $com['address'] = I('post.address', '', 'htmlspecialchars');
        $com['bank'] = I('post.bank', '', 'htmlspecialchars');
        $com['about'] = I('post.about', '', 'htmlspecialchars');
        $count = $company->save($com);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/company"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function addcom()
    {
        $company = M('company');
        $company->name = I('post.name', '', 'htmlspecialchars');
        $company->person = I('post.person', '', 'htmlspecialchars');
        $company->telephone = I('post.telephone', '', 'htmlspecialchars');
        $company->address = I('post.address', '', 'htmlspecialchars');
        $company->bank = I('post.bank', '', 'htmlspecialchars');
        $company->about = I('post.about', '', 'htmlspecialchars');
        if ($company->name != null and  $company->person != null and  $company->telephone != null and  $company->address != null) {
            $company->add();
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/company"));
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    public function type()
    {
        $projecttype = M('projecttype');
        $this->list = $projecttype->select();
        $this->display();
    }


    public function addtype()
    {
        $projecttype = M('projecttype');
        $projecttype->name = I('post.type', '', 'htmlspecialchars');
        if ($projecttype->name != null) {
            $projecttype->add();
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/type"));
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }


    public function tasktype()
    {
        $tasktype = M('tasktype');
        $where['parentid'] = 0;
        $this->li = $tasktype->where($where)->select();

        $this->list = $tasktype->select();
        $this->display();
    }

    public function addtasktype()
    {
        $tasktype = M('tasktype');
        $parid = I('post.parentid');
        $where['id'] = $parid;
        $arr = $tasktype->where($where)->find();

        $data["name"] = I('post.name');
        $data['parentid'] = $parid;
        $data['level'] = $arr['level'] + 1;
        $data['time'] = I('post.time');

        if ($tasktype->name != null) {
            //  dump($tasktype);exit();
            $tasktype->add($data);
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/tasktype"));
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    public function deltask()
    {
        $tasktype = M('tasktype');
        $id = $_GET['id'];
        $count = $tasktype->delete($id);
        if ($count > 0) {

            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function updatetask()
    {
        $tasktype = M('tasktype');
        $id = $_GET['id'];
        $arr = $tasktype->find($id);
        $this->assign('tasktype', $arr);
        $this->display();
    }

    public function  changetask()
    {
        $tasktype = M('tasktype');
        $task['id'] = I('post.id', '', 'htmlspecialchars');
        $task['name'] = I('post.name', '', 'htmlspecialchars');
        $task['time'] = I('post.time', '', 'htmlspecialchars');
        $count = $tasktype->save($task);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/tasktype"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function del()
    {
        $projecttype = M('projecttype');
        $id = $_GET['id'];
        $count = $projecttype->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function update()
    {
        $projecttype = M('projecttype');
        $id = $_GET['id'];
        $arr = $projecttype->find($id);
        $this->assign('projecttype', $arr);
        $this->display();
    }

    public function  change()
    {
        $projecttype = M('projecttype');
        $pro['id'] = I('post.id', '', 'htmlspecialchars');
        $pro['name'] = I('post.type', '', 'htmlspecialchars');
        $count = $projecttype->save($pro);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/type"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function role()
    {
        $role = M('role');
        $data=$role->order('desorder')->select();
        $this->list =$data;
        $this->rolelist=$data;
        $this->display();
    }

    public function addrole()
    {
        $role = M('role');
        $role->name = I('post.name', '', 'htmlspecialchars');
        $role->rules = I('post.rules', '', 'htmlspecialchars');
        $role->firstpage = I('post.firstpage', '', 'htmlspecialchars');
        $role->remark = I('post.remark', '', 'htmlspecialchars');
        $role->desorder = I('post.desorder', '', 'htmlspecialchars');
        $role->pid = I('post.pid', '', 'htmlspecialchars');
        if ($role->name != null and $role->rules != null) {
            $role->add();
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/role"));
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    public function delrole()
    {
        $role = M('role');
        $id = $_GET['id'];
        $count = $role->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function updaterole()
    {
        $role = M('role');
        $id = $_GET['id'];
        $arr = $role->find($id);
        $this->assign('role', $arr);
        $this->display();
    }

    public function  changerole()
    {
        $role = M('role');
        $rol['id'] = I('post.id', '', 'htmlspecialchars');
        $rol['name'] = I('post.name', '', 'htmlspecialchars');
        $rol['status'] = I('post.status', '', 'htmlspecialchars');
        $rol['rules'] = I('post.rules', '', 'htmlspecialchars');
        $count = $role->save($rol);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("xitong/role"));
            $this->success("修改成功");
        } else {
            $this->error("修改失败或未修改");
        }
    }

    public function dayworktype()
    {
    	$dayworktype=M('dayworktype');
    	$this->list=$dayworktype->select();
    	$this->display();
    } 
    
    public function adddayworktype()
    {        
        $dayworktype=M('dayworktype');
        $dayworktype->name=I('post.type','','htmlspecialchars');
        if($dayworktype->name!=null)
        {
        	$dayworktype->add();
        	$this->assign("waitSecond",1);
        	$this->assign("jumpUrl",U("xitong/dayworktype"));
        	$this->success("添加成功");        	
        }
        else 
        {
        	$this->error("添加失败");
        }   
    }
    
    public function deldayworkype()
    {        
        $dayworktype=M('dayworktype');
        $id=I('get.id');
        $count=$dayworktype->delete($id);
        if($count>0)
        {
        	$this->success("删除成功");
        }
        else
        {
        	$this->error("删除失败");
        }
    }
    
    public function updateDayworktype()
    {      
        $dayworktype=M('dayworktype');
        $id=I('get.id');
        $arr=$dayworktype->find($id);
        $this->assign('dayworktype',$arr);
        $this->display();       
    }
    
    public function saveDayworktype()
    {        
        $dayworktype=M('dayworktype');
        $type["id"]=I("post.id",'','htmlspecialchars');
        $type["name"]=I("post.type",'','htmlspecialchars');
        $count=$dayworktype->save($type);
        if($count>0)
        {
        	$this->assign("waitSecond",1);
        	$this->assign("jumpUrl",U("xitong/dayworktype"));
        	$this->success("修改成功");
        }
        else
        {
        	$this->error("修改失败");
        }
    }

    public function setconfig()
    {
        $customDay=C("customDay");
        $this->assign("customDay",$customDay);
        $custtypes=C("custtypes");
        $this->assign("custtypes",$custtypes);


        $custtypedata=M("custtype");
        $custtype=$custtypedata->select();
        $this->custtype=$custtype;

        $this->display();
    }

    public function saveconfig()
    {
        $file=$this->_post('files');
        unset($_POST['files']);

        //dump($_POST["custtypes"]);exit;

        unset($_POST["custtype"]);
        if($this->update_config($_POST,"./Common/".$file)){
            $this->success('操作成功',U('xitong/setconfig'));
        }else{
            $this->success('操作失败',U('xitong/setconfig'));
        }

    }

    private function update_config($config, $config_file = '')
    {
        !is_file($config_file) && $config_file ='./Common/siteconfig.php';
        if (is_writable($config_file)) {
            //$config = require $config_file;
            //$config = array_merge($config, $new_config);
            //dump($config);EXIT;
            file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);
            @unlink(RUNTIME_FILE);
            return true;
        } else {

            return false;
        }
    }

    public function _empty()
    {
        $this->error("你访问的方法不存在！");
    }


}

?>